package com.ruoyi.eqmonitor.dispose.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.eqmonitor.domain.OptDeviceAlarm;
import com.ruoyi.eqmonitor.dtos.OptDeviceDto;
import com.ruoyi.eqmonitor.service.IOptDeviceAlarmService;
import com.ruoyi.eqmonitor.service.IOptDeviceService;
import com.ruoyi.eqmonitor.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 设备离线处理程序
 */
@Slf4j
@Component
@Profile("prod")
public class DeviceOffLineTask {

    @Resource
    private IOptDeviceService iOptDeviceService;

    @Resource
    private IOptDeviceAlarmService iOptDeviceAlarmService;

    /**
     * 每小时执行一次 判断设备是否离线
     */
    @Scheduled(cron = "0 5 0/1 * * ?")
    public void validateDeviceOffLine() {
        Date now = new Date();
        List<OptDeviceDto> optDeviceDtos = iOptDeviceService.selectOptDeviceListAndLastTime(new OptDeviceDto());
        for (OptDeviceDto optDeviceDto : optDeviceDtos) {
            OptDeviceAlarm newAlarm = new OptDeviceAlarm();
            newAlarm.setDeviceName(optDeviceDto.getDeviceName());
            newAlarm.setDeviceCode(optDeviceDto.getDeviceCode());
            newAlarm.setSiteName(optDeviceDto.getSiteName());
            newAlarm.setDeviceType(optDeviceDto.getDeviceType());
            newAlarm.setInstallTime(optDeviceDto.getInstallTime());
            newAlarm.setAbx(optDeviceDto.getAbx());
            newAlarm.setAby(optDeviceDto.getAby());
            // 设备故障
            newAlarm.setAlarmType(Constant.AlarmType.DEVICE_FAULT.getValue());
            // 设备掉线
            newAlarm.setAlarmPhenomenon(Constant.AlarmPhenomenon.OFF_LINE.getValue());
            newAlarm.setConfirmAlarm(Constant.ConfirmAlarmType.UNCONFIRMED.getValue());
            newAlarm.setAlarmCode(StringUtils.EMPTY);
            newAlarm.setPersistentStatus(Constant.TRUE);
            Date deviceLastTime = optDeviceDto.getDeviceLastTime();
            Integer offLineTime = optDeviceDto.getOffLineTime();
            // 没设置掉线周期 则不处理
            if (ObjectUtil.isNull(offLineTime) || offLineTime == -1) continue;
            long between = -1;
            if (deviceLastTime != null) {
                between = DateUtil.between(now, deviceLastTime, DateUnit.HOUR);
            }
            // 没有回传时间 || 回传时间 超出了设备掉线周期的时间 则报警
            if (between == -1 || between > offLineTime) {
                newAlarm.setAlarmCodeValue((double) between);
                newAlarm.setAlarmCount(1);
                newAlarm.setAlarmTime(now);
                newAlarm.setLastAlarmTime(now);
                newAlarm.setConfigAlarmValue(Double.valueOf(offLineTime));
                newAlarm.setConfigFloorLimit(null);
                newAlarm.setConfigUpperLimit(null);
                newAlarm.setPersistentStatus(Constant.TRUE);
                log.info("设备: {}, 最后在线时间: {}, 掉线周期: {}, 当前时间: {}, 发生设备掉线, 掉线时间: {}", optDeviceDto.getDeviceCode(), optDeviceDto.getDeviceLastTime(), optDeviceDto.getOffLineTime(), now, between);
                iOptDeviceAlarmService.doSaveAlarm(newAlarm);
            } else {
                iOptDeviceAlarmService.updatePersistentStatus(newAlarm);
            }
        }
    }
}
